package com.lpf.json.fastjson.base;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

public class TestBase {

	public static void main(String[] args) {
		Group group = new Group();
		group.setId(0L);
		group.setName("admin");

		User guestUser = new User();
		guestUser.setId(2L);
		guestUser.setName("guest");

		User rootUser = new User();
		rootUser.setId(3L);
		rootUser.setName("root");

		group.addUser(guestUser);
		group.addUser(rootUser);

		List<User> userList = group.getUsers();

		// Group序列化
		String jsonString = JSON.toJSONString(group);
		System.out.println("Group序列化:" + jsonString);

		// Group反序列化
		jsonString = "{\"id\":0,\"name\":\"admin\",\"users\":[{\"id\":2,\"name\":\"guest\"},{\"id\":3,\"name\":\"root\"}]}";
		group = JSON.parseObject(jsonString, Group.class);
		System.out.println("Group反序列化:" + group);

		// List序列化
		jsonString = JSON.toJSONString(userList);
		System.out.println("List序列化:" + jsonString);

		// List泛型反序列化
		jsonString = "[{\"id\":2,\"name\":\"guest\"},{\"id\":3,\"name\":\"root\"}]";
		userList = JSON.parseObject(jsonString, new TypeReference<List<User>>() {});
		System.out.println("List反序列化:" + userList);
	}
}
